
//
//  MenuProvinceView.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/29.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "MenuCityView.h"
#import "MenuCityTableView.h"
#import "MenuCenterCell.h"
#import "UIView+ZF.h"
#import "ProviceModel.h"
#import "CityModel.h"
#define kRowH 44
@interface MenuCityView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) MenuCityTableView *menuCityTabView;
@property(nonatomic,strong) NSMutableArray *proviceMutb;
@property(nonatomic,strong) NSMutableArray *cityMutb;
@property(nonatomic,assign) NSInteger row;
@end
@implementation MenuCityView
static NSString *identifierMenuCenterCell = @"menuCenterCell";
-(NSMutableArray *)proviceMutb
{
    if (!_proviceMutb) {
        _proviceMutb = [NSMutableArray array];
        NSString *filePatch = [[NSBundle mainBundle]pathForResource:@"ProvincesAndCities.plist" ofType:nil];
        NSArray *temp = [NSArray arrayWithContentsOfFile:filePatch];
        //        XLog(@"temp:%@",temp);
        for (NSDictionary *dict in temp) {
            ProviceModel *proviceModel = [ProviceModel proviceModelWithDictionary:dict];
            [_proviceMutb addObject:proviceModel];
        }
        XLog(@"self.proviceMutb:%@",self.proviceMutb);
    }
    return _proviceMutb;
}
-(NSMutableArray *)cityMutb
{
    if (!_cityMutb) {
        _cityMutb = [NSMutableArray array];
    }
    return _cityMutb;
}
- (MenuCityTableView *)menuCityTabView
{
    if (!_menuCityTabView) {
        CGRect frame = CGRectMake(0.f, 0, self.w, self.h);
        _menuCityTabView = [[MenuCityTableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        _menuCityTabView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
        _menuCityTabView.delegate = self;
        _menuCityTabView.dataSource = self;
        _menuCityTabView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
        [_menuCityTabView registerNib:[UINib nibWithNibName:@"MenuCenterCell" bundle:nil] forCellReuseIdentifier:identifierMenuCenterCell];
    }
    return _menuCityTabView;
}
- (void)setH:(CGFloat)h
{
    _h = h;
    _menuCityTabView.h = h;
}
- (void)setW:(CGFloat)w
{
    _w = w;
    _menuCityTabView.w = w;
}
- (void)setCityNamed:(NSString *)cityNamed
{
    _cityNamed = cityNamed;
    
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = kOrangeBg;
        [self addSubview:self.menuCityTabView];
    }
    return self;
}- (void)setIndex:(NSInteger)index
{
    _index = index;
    XLog(@"index:%ld",index);
}
- (void)setPreIndex:(NSInteger)preIndex
{
    _preIndex = preIndex;
}
-(void)reloadData
{
    ProviceModel *proviceModel = self.proviceMutb[self.preIndex];
    XLog(@"reloadData");
    XLog(@"self.preIndex:%ld",self.preIndex);
//    CityModel *cityModel = proviceModel.Cities[indexPath.row];
    for (CityModel *cityModel in proviceModel.Cities) {
//        if (!proviceModel.show) {
        XLog(@"city:%@",cityModel.city);
            cityModel.show = NO;
//        }
    }
    [self.menuCityTabView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    ProviceModel *proviceModel = self.proviceMutb[self.index];
//    CityModel *cityModel = proviceModel.Cities[0];
    return proviceModel.Cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    MenuCenterCell *cell = [tableView dequeueReusableCellWithIdentifier:identifierMenuCenterCell forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1];
    ProviceModel *proviceModel = self.proviceMutb[self.index];
    CityModel *cityModel = proviceModel.Cities[indexPath.row];
    cell.cityNamed = self.cityNamed;
    cell.cityModel = cityModel;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kRowH;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
//    ProviceModel *proviceModel = self.proviceMutb[indexPath.row];
//    proviceModel.show = YES;
//    if (self.returnValueBlock) {
//        self.returnValueBlock(indexPath.row, proviceModel.State);
//    };
    ProviceModel *proviceModel = self.proviceMutb[self.index];
    CityModel *cityModel = proviceModel.Cities[indexPath.row];
    CityModel *cityModelPre = proviceModel.Cities[self.row];
    cityModel.show = YES;
    if (self.row != indexPath.row) {
        cityModelPre.show = NO;
    }
    self.row = indexPath.row;
    [self.menuCityTabView reloadData];
    
    
    if (self.returnValueBlock) {
        self.returnValueBlock(indexPath.row, cityModel.city);
    };
}
@end
